import React, { Component } from 'react'
import { Card, Breadcrumb, Form, Radio, Button, DatePicker, Table, Tag, Space, Modal, message } from 'antd'
import { Link } from 'react-router-dom'
import { ArticleStatus } from 'api/constants'
import { delArticle, getArticles } from 'api/article'
import defaultImg from 'assets/error.png'
import { DeleteOutlined, EditOutlined, ExclamationCircleOutlined } from '@ant-design/icons'
import ChannelList from 'components/ChannelList'

const { RangePicker } = DatePicker

class ArticleList extends Component {
  state = {
    articles: {},
  }
  reqParams = {
    page: 1,
    per_page: 10,
  }
  columns = [
    {
      title: '封面',
      render(data) {
        return data.cover.type === 0 ? (
          <img
            src={defaultImg}
            alt=''
            style={{ width: 200, height: 120, objectFit: 'cover' }}
          />
        ) : (
          <img
            src={data.cover.images[0]}
            alt=''
            style={{ width: 200, height: 120, objectFit: 'cover' }}
          />
        )
      },
    },
    {
      title: '标题',
      dataIndex: 'title',
    },
    {
      title: '状态',
      dataIndex: 'status',
      render(status) {
        const obj = ArticleStatus.find((item) => item.id === status)
        return <Tag color={obj.color}>{obj.status}</Tag>
      },
    },
    {
      title: '发布时间',
      dataIndex: 'pubdate',
    },
    {
      title: '阅读数',
      dataIndex: 'read_count',
    },
    {
      title: '评论数',
      dataIndex: 'comment_count',
    },
    {
      title: '点赞数',
      dataIndex: 'like_count',
    },
    {
      title: '操作',
      render: ({ id }) => {
        return (
          <Space>
            <Button
              type='primary'
              shape='circle'
              icon={<EditOutlined />}
              onClick={() => this.editArticle(id)}
            />
            <Button
              type='primary'
              shape='circle'
              icon={<DeleteOutlined />}
              danger
              onClick={() => this.delArticle(id)}
            />
          </Space>
        )
      },
    },
  ]

  render() {
    const { total_count, results, per_page, page } = this.state.articles
    return (
      <div>
        <Card
          title={
            <Breadcrumb>
              <Breadcrumb.Item>
                <Link to={'/home'}>首页</Link>
              </Breadcrumb.Item>
              <Breadcrumb.Item>内容管理</Breadcrumb.Item>
            </Breadcrumb>
          }
        >
          <Form
            initialValues={{ status: -1, channel_id: 0 }}
            onFinish={this.onFinish}
          >
            {/* 状态选择 */}
            <Form.Item
              name='status'
              label='状态'
            >
              <Radio.Group>
                {ArticleStatus.map((item) => {
                  return (
                    <Radio
                      value={item.id}
                      key={item.id}
                    >
                      {item.status}
                    </Radio>
                  )
                })}
              </Radio.Group>
            </Form.Item>
            {/* 状态选择 */}
            {/* 频道选择 */}
            <Form.Item
              name={'channel_id'}
              label='频道'
            >
              <ChannelList></ChannelList>
            </Form.Item>
            {/* 频道选择 */}
            {/* 日期选择 */}
            <Form.Item name={'date'}>
              <RangePicker />
            </Form.Item>
            {/* 日期选择 */}
            {/* 筛选按钮 */}
            <Form.Item>
              <Button
                type='primary'
                htmlType='submit'
              >
                筛选
              </Button>
            </Form.Item>
            {/* 筛选按钮 */}
          </Form>
        </Card>
        <Card title={`根据条件一共筛选出${total_count}条数据`}>
          <Table
            columns={this.columns}
            dataSource={results}
            rowKey='id'
            pagination={{ pageSize: per_page, total: total_count, current: page, showSizeChanger: true, pageSizeOptions: [2, 5, 10, 15], onChange: this.onChange }}
          />
        </Card>
      </div>
    )
  }
  componentDidMount() {
    this.getArticleList()
  }

  async getArticleList() {
    const { data } = await getArticles(this.reqParams)
    this.setState({
      articles: data,
    })
  }
  onChange = (page, pageSize) => {
    this.reqParams.page = page
    this.reqParams.per_page = pageSize
    this.getArticleList()
  }
  onFinish = ({ status, channel_id, date }) => {
    console.log(date)
    if (status !== -1) {
      this.reqParams.status = status
    } else {
      delete this.reqParams.status
    }
    if (channel_id !== undefined) {
      this.reqParams.channel_id = channel_id
    } else {
      delete this.reqParams.channel_id
    }
    if (date !== undefined && date !== null) {
      this.reqParams.begin_pubdate = date[0].startOf('day').format('YYYY-MM-DD HH-mm-ss')
      this.reqParams.end_pubdate = date[1].endOf('day').format('YYYY-MM-DD HH-mm-ss')
    } else {
      delete this.reqParams.begin_pubdate
      delete this.reqParams.end_pubdate
    }
    this.getArticleList()
  }
  delArticle(id) {
    Modal.confirm({
      title: '删除提示',
      icon: <ExclamationCircleOutlined />,
      content: '确定要删除文章吗',
      okText: 'Yes',
      okType: 'danger',
      cancelText: 'No',
      onOk: async () => {
        try {
          await delArticle(id)
          message.success('删除成功')
          this.getArticleList()
        } catch (error) {
          message.error('删除失败', error)
          console.log(error)
        }
      },
    })
  }
  editArticle(id) {
    this.props.history.push(`/home/publish/${id}`)
  }
}

export default ArticleList
